using System;
using System.Collections.Generic;
using System.Text;
using MWang.Data.ORM;
using MWang.Data;
using CIM.Model;

namespace CIM.BLL
{
    public  class BankActive:logiclayer<BankActiveInfo>
    {
        public BankActive(string connstr):base(connstr,false)
        {

        }

        public List<BankActiveInfo> GetAllBankActives()
        {
            return GetItemsByDataTable(db.Select("select * from " + tableInfo.TableName + " order by executeDate,loanterm"));
        }

        public List<BankActiveInfo> GetItemsByExecuteDate(DateTime dt)
        {
            return GetItemsByDataTable(db.Select("select * from "+tableInfo.TableName+" where datediff(\"d\",executedate,'"+dt.ToShortDateString()+"')=0"));
        }

        public BankActiveInfo GetItemByLoanInfo(DateTime getloandate, int loanterm)
        {
            string sql = "select top 1 * from "+tableInfo.TableName+" where datediff(\"d\",executedate,'"+getloandate.ToShortDateString()+"')>=0 and loanterm="+loanterm+" order by executedate desc";
            List<BankActiveInfo> list = GetItemsByDataTable(db.Select(sql));
            if (list.Count > 0)
            {
                return list[0];
            }
            return null;
        }

        public BankActiveInfo GetItemByDateAndTerm(DateTime execDate, int loanterm)
        {
            List<BankActiveInfo> list=GetItemsByDataTable(db.Select("select * from "+tableInfo.TableName+" where datediff(\"d\",executeDate,'"+execDate.ToShortDateString()+"')=0 and loanterm="+loanterm));
            return list.Count > 0 ? list[0] : null;
        }

        public int RemoveByDateAndTerm(DateTime execDate, int loanterm)
        {
            string sql = "delete from "+tableInfo.TableName+" where datediff(\"d\",executeDate,'"+execDate.ToShortDateString()+"')=0 and loanterm="+loanterm;
            return db.ExecuteNonQuery(sql);
        }
    }
}
